<?php

/**
 * 调用WEB支付接口(通联收银台)
 * @author chenshubo
 */
namespace App\Service\OrderService\OrderInterfaceService;
use App\Service\BaseService;
use App\Service\Code\Normal;
use App\Model\PssModel\OrderSupplier;
use App\Model\PssModel\Order;

class OrderWebCashPay extends BaseService
{
    //支付客户端
    public $payClient = [
        3 => 'App\\Service\\VendorClient\\AlinpayApi\\AlinpayApiClient',//通联支付客户端
    ];
    
    /**
     * 更新商户提交订单支付信息进行支付
     * @param array $param
     */
    public function index( $param = [] ) 
    {
        $requestData = $param['requestData'];        
        if( empty($requestData['orderSn']) ) {
            return Normal::returnCode(Normal::FAIL,"orderSn不能为空");
        }
        
        if( empty($requestData['orderMoney']) ) {
            return Normal::returnCode(Normal::FAIL,"orderMoney不能为空");
        }
        
        if( empty($requestData['notifyUrl']) ) {
            return Normal::returnCode(Normal::FAIL,"notifyUrl不能为空");
        }
        
        if( empty($requestData['frontUrl']) ) {
            return Normal::returnCode(Normal::FAIL,"frontUrl不能为空");
        }
        
        if( empty($requestData['payWay']) ) {
            return Normal::returnCode(Normal::FAIL,"payWay不能为空");
        }
        
        $orderSn    = checkFields($requestData['orderSn']);
        $orderMoney = checkFields($requestData['orderMoney']);
        $notifyUrl  = $requestData['notifyUrl'];
        $frontUrl   = $requestData['frontUrl'];
        $payWay    = checkFields($requestData['payWay']);
        if( $orderSn != $requestData['orderSn'] ) {
            return Normal::returnCode(Normal::FAIL,"orderSn订单号不能包含特殊字符");
        }
        
        if ( !isset($this->payClient[$payWay]) ) {
            return Normal::returnCode(Normal::FAIL,"payWay不存在");
        }
        
        $bid = $param['binfo']['bid'];
        $orderInfo = OrderSupplier::instance()->info(['bid' => $bid,'order_sn'  => $orderSn],['id','order_status']);
        if( empty($orderInfo) ) {
            return Normal::returnCode(Normal::FAIL,"当前订单【{$orderSn}】信息不存在");
        }
        
        if( $orderInfo['order_status'] != 0 ) {
            return Normal::returnCode(Normal::FAIL,"当前订单【{$orderSn}】不是待付款状态");
        }
        
        $real_total_money = OrderSupplier::instance()->where('bid',$bid)->where('order_sn',$orderSn)->sum('real_total_money');
        if( bccomp($real_total_money, $orderMoney,2) != 0 ) {
            return Normal::returnCode(Normal::FAIL,"当前订单支付金额不正确【应支付：{$real_total_money}，实际支付：{$orderMoney}】");
        }
        
        $payClientClass = $this->payClient[$payWay];
        if( !class_exists($payClientClass) ) {
            return Normal::returnCode(Normal::FAIL,"当前支付类{$payWay}不存在");
        }
        
        $res = $payClientClass::instance()->OrderPay([
            'trxamt'        => $orderMoney,
            'reqsn'         => $orderSn,
            'returl'        => $frontUrl,
            'notify_url'    => config('api.alinpay.acount.notifyUrl'),
            'remark'        => $bid,
        ]);
        
        if( $res['code'] != Normal::SUC ) {
            return $res;
        }
        
        //同步主订单支付地址信息
        $where = [
            'bid'       => $bid,
            'order_sn'  => $orderSn,
        ];
        
        $data = [
            'pay_way'       => $payWay,
            'front_url'     => $frontUrl,
            'notify_url'    => $notifyUrl,
            'md'            => time()
        ];
        
        //更新支付地址信息
        if( !Order::instance()->edit($where,$data) ) {
            Normal::returnCode(Normal::FAIL,"当前订单信息不存在");
        }
        
        return $res;
    }
}